package com.HW;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : TAlibaba2
 * @Author : kele
 * @Date: 2023/10/22 13:08
 * @Description : 阿里巴巴找黄金宝箱(II)
 */
public class TAlibaba2 {

    public static int num;

    public static void main(String[] args) {
        handle("1,1,1,1,3,3,3,6,6,8");

    }

    public static void handle(String str) {

        String[] split = str.split(",");

        HashMap<String, Integer> map = new HashMap<>();

        for (String s : split) {
            Integer value = map.getOrDefault(s, 0);
            value++;
            map.put(s, value);
        }

        int[] ints = map.entrySet().stream().sorted(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        }).mapToInt(x -> x.getValue()).toArray();


        int sum = 0;
        for (int i = 0; i < ints.length; i++) {
            sum += ints[i];
            if (sum >= split.length / 2) {
                System.out.println(i + 1);
                break;
            }
        }

    }
}
